home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * SpoolUtils.C *
- * *
- * Line Printer Daemon using TCP/IP printer protocol *
- * *
- * -------------- The spool file maintenance routines -------------- *
- * *
- * Written by Casper Boon, July, 1992. *
- * *
- * © 1992, Casper Boon. *
- * *
- ************************************************************************/
-
- #include "LPD.H"
- #include "Spooler.H"
- #include "TCPStream.H"
- #include "lpdProtos.H"
-
-
- integer spool_dir, spool_file;
- extern Boolean printing;
-
- char *cfA = "cfA";
-
- Str255 SpoolFolder = "\pSpool Folder";
-
-
- /************************************************************************
- ************************************************************************/
- void CreateSpoolFile(char *name, char*printer)
- {
- char *end, l;
- OSErr err;
- integer rRef, tVol;
- Handle tH;
-
- SetUpSpoolDir();
- while (*name && *name != 'c' && *name != 'd') name++;
- if DEBUGGING
- log_printf("Creating spool file %s\n", name);
-
- end = name;
- while (*end && *end != LF) end++;
- *end = 0;
- if (!(l = strlen(name)))
- return; /* no name */
- *--name = l;
-
- Create((StringPtr)name, spool_dir, 'SIMN', 'TEXT');
- if (err = FSOpen((StringPtr)name, spool_dir, &spool_file))
- log_printf("Error creating spool file %s %d\n", name, err);
-
- if (name[1] == cfA[0]) /* control file */
- {
- end = printer;
- while (*end && *end != LF) end++;
- *end = 0;
-
- GetVol(NIL, &tVol);
- SetVol(NIL, spool_dir);
- CreateResFile((StringPtr)name);
- rRef = OpenResFile((StringPtr)name);
- SetVol(NIL, tVol);
-
- tH = NewHandle(strlen(printer) + 1);
- HLock(tH);
- BlockMove(printer, (*tH), strlen(printer)+1);
- HUnlock(tH);
- AddResource(tH, 'PRTR', 0, NIL);
- CloseResFile(rRef);
- DisposHandle(tH);
- }
- }
-
- /************************************************************************
- ************************************************************************/
- void DeleteSpoolFile(StringPtr name)
- {
- integer count = 0, cfRef, tRef;
- Str63 buf;
- Str255 prt_name_buf;
-
-
- if (FSOpen(name, spool_dir, &cfRef))
- {
- log_printf(
- "The control file %p disappeared before I could remove it?\n",
- name);
- return; /* error opening control file */
- }
-
- while ( ReadLine(cfRef, (char*)buf, 61) )
- {
- if (buf[0] != 'U') /* the file to unlink */
- continue;
- BlockMove(&buf[1], &prt_name_buf[1], strlen((char*)&buf[1]));
- prt_name_buf[0] = strlen((char*)&buf[1]);
- if DEBUGGING
- log_printf("Dequeue %p\n", prt_name_buf);
-
- if (FSDelete(prt_name_buf, spool_dir))
- { /* the delete failed so try closing first */
- if ( FSGetFRef(prt_name_buf, spool_dir, &tRef) == noErr )
- {
- FSClose(tRef);
- FSDelete(prt_name_buf, spool_dir);
- }
- }
- }
-
- FSClose(cfRef);
- if DEBUGGING
- log_printf("Dequeue %p\n", name);
-
- #if 1
- FSDelete(name, spool_dir);
- #else
- log_printf("Renaming %p\n", name);
- BlockMove(name, prt_name_buf, prt_name_buf[0]+1);
- prt_name_buf[1] = '_';
- Rename(name, spool_dir, prt_name_buf); // rename the new one
- #endif
- }
-
- /************************************************************************
- ************************************************************************/
- void WriteSpoolFile(char*data, integer len)
- {
- LongInt count = len;
- if (spool_file) FSWrite(spool_file, &count, data);
- }
-
- /************************************************************************
- ************************************************************************/
- void CloseSpoolFile()
- {
- if (spool_file) FSClose(spool_file);
- spool_file = 0;
- }
-
- static Boolean last_was_cr = FALSE;
-
- char ReadChar(integer fRef);
- /************************************************************************
- ************************************************************************/
- char ReadChar(integer fRef)
- {
- char c = fgetc(fRef);
- if ( c == CR )
- {
- last_was_cr = TRUE;
- return LF;
- }
- if ( c == LF && last_was_cr )
- { /* we ignore LF's after CR's */
- c = fgetc(fRef);
- if ( c == CR )
- {
- last_was_cr = TRUE;
- return LF;
- }
- }
- last_was_cr = FALSE;
- return c;
- }
-
-
- /************************************************************************
- ************************************************************************/
- integer ReadLine(integer fRef, char *buf, integer lim)
- {
- integer i = 0;
- char c;
- if (!buf)
- {
- while ((c=ReadChar(fRef)) && (c != LF)) ; /* skip the line */
- return c;
- }
- while ((c=ReadChar(fRef)) && (c != LF))
- {
- *buf++ = c;
- if (i++ == lim)
- { /*that was number 31 */
- while ((c=ReadChar(fRef)) && (c != LF)) ; /* skip the rest */
- break; /* and get out of the loop */
- }
- }
- *buf = 0;
- return i;
- }
-
- /************************************************************************
- ************************************************************************/
- integer ReadCtlFile(StringPtr name, ctl_def * ctl)
- {
- char c;
- integer tVol, rRef, fRef;
- Handle tH;
- LongInt size;
-
- ctl->nFiles = 0;
- ctl->printer[0] = 0;
- ctl->class[0] = 0;
- ctl->host[0] = 0;
- ctl->src_name[0] = 0;
- ctl->owner[0] = 0;
-
- GetVol(NIL, &tVol);
- SetVol(NIL, spool_dir);
- rRef = OpenResFile((StringPtr)name);
- SetVol(NIL, tVol);
-
- if (rRef != -1) /* which would indicate an error on the resource fork */
- {
- tH = GetResource('PRTR', 0);
- size = GetHandleSize(tH);
- HLock(tH);
- BlockMove((*tH), ctl->printer, size);
- HUnlock(tH);
- CloseResFile(rRef);
- }
- else
- {
- ctl->printer[0] = 'l'; ctl->printer[1] = 'p'; ctl->printer[2] = 0;
- }
-
- if (FSOpenRO(name, spool_dir, &fRef))
- return -1;
-
- while (c = ReadChar(fRef))
- {
- switch (c)
- {
- case 'C' : ReadLine(fRef, ctl->class, 30);
- break;
- case 'H' : ReadLine(fRef, ctl->host, 30);
- break;
- case 'l' :
- case 'f' : if (ctl->src_name[0])
- strcat(ctl->src_name, ",...");
- else
- ReadLine(fRef, ctl->src_name, 30);
- ctl->nFiles++;
- break;
- case 'P' : ReadLine(fRef, ctl->owner, 30);
- break;
- default: ReadLine(fRef, NIL, 30);
- break;
- }
- }
-
- FSClose(fRef);
- return 0;
- }
-
-
- /************************************************************************
- ************************************************************************/
- void LogError(char *rbuff, Handle elog)
- {
- LongInt len = GetHandleSize(elog);
- LongInt slen = strlen(rbuff);
- SetHandleSize(elog, len+slen);
- HLock(elog);
- BlockMove(rbuff, &(*elog)[len], slen);
- HUnlock(elog);
- }
-
-
- /************************************************************************
- ************************************************************************/
- void SetUpSpoolDir()
- {
- integer wdRef;
- LongInt wdID;
- integer vol;
-
- GetWDir(&wdRef, &wdID, &vol);
-
- SetToBlessedFolder(SpoolFolder);
-
- GetVol(0L, &spool_dir);
-
- SetWDir(wdRef, wdID, vol);
- }
-
-
- /************************************************************************
- ************************************************************************/
- integer GetLogDir()
- {
- integer wdRef, log_dir;
- LongInt wdID;
- integer vol;
-
- GetWDir(&wdRef, &wdID, &vol);
-
- SetToBlessedFolder(SpoolFolder);
-
- GetVol(0L, &log_dir);
-
- SetWDir(wdRef, wdID, vol);
- return log_dir;
- }
-
-
- /************************************************************************
- ************************************************************************/
- Boolean cfA_file(StringPtr nm);
- Boolean cfA_file(StringPtr nm)
- {
- integer l = strlen(cfA);
- if (nm[0] < l) return FALSE;
- do {
- if (nm[l] != cfA[l-1]) return FALSE;
- }
- while (--l);
- return TRUE;
- }
-
-
- /************************************************************************
- ************************************************************************/
- integer EnumerateFiles(integer wdRef, integer *anIndex,
- StringPtr name_buf, ctl_def *ctl)
- {
- WDPBRec myWDPB;
- CInfoPBRec myCIPB;
- integer retVal = FALSE, osVal, fRef,
- index = 1;
- Byte *nm;
-
- if (anIndex) index = *anIndex;
-
- myCIPB.dirInfo.ioVRefNum = wdRef;
- myCIPB.dirInfo.ioNamePtr = name_buf;
- myCIPB.dirInfo.ioFDirIndex = index++;
- myCIPB.dirInfo.ioDrDirID = 0;
- name_buf[0]=0;
-
- /* get info about object with this name */
- while ( ! PBGetCatInfo(&myCIPB, FALSE) )
- {
- if ( !(myCIPB.dirInfo.ioFlAttrib & 0x10) )
- { /* it's not a directory */
- nm = myCIPB.dirInfo.ioNamePtr;
- if ( cfA_file(nm) )
- {
- /* first check that there is a control file for it */
- if ( ReadCtlFile(nm, ctl) )
- {
- /* there aint no ctl file */
- log_printf("Found a file to print, but can't open it\n");
- }
- else
- {
- if (anIndex) *anIndex = index;
- return -1;
- }
- }
- }
-
- myCIPB.dirInfo.ioFDirIndex = index++;
- myCIPB.dirInfo.ioDrDirID = 0;
- name_buf[0] = 0;
- }
-
- return 0;
- }
-
-
- /************************************************************************
- ************************************************************************/
-
- #define SendData(s) PutData(crefnum, (Ptr)&s[1], s[0])
- #define SendCData(s,c) PutData(crefnum, (Ptr)s, c)
-
- void PutData(integer crefnum, Ptr data, Word length);
- void PutData(integer crefnum, Ptr data, Word length)
- {
- integer state;
-
- TCPSWrite(crefnum, data, length, &state);
-
- WaitForState(&state);
- }
-
-
- /************************************************************************
- ************************************************************************/
- void SendJobs(integer crefnum)
- {
- integer index = 1, count = 0;
- char lf = 0x0A;
- StringPtr stat;
- StringPtr prName;
- Str255 name_buf;
- ctl_def ctl;
-
- while ( EnumerateFiles(spool_dir, &index, name_buf, &ctl) )
- { /* files found */
- if (!count++)
- {
- if (printing)
- {
- SendData("\pLaserWriter <");
- if (prName=GetPRName())
- SendData(prName);
- SendData("\p>\012");
- if ((stat = PrinterStatus(prName)) && stat[0])
- SendData(stat);
- else if (!stat)
- SendData("\pnot available");
- else
- SendData("\pis printing");
- SendCData(&lf, 1);
- }
- SendData("\pOwner Job File\012");
- }
- SendCData(ctl.owner, strlen(ctl.owner));
- SendCData(" ", 12-strlen(ctl.owner));
- SendCData((char*)&name_buf[4], 3);
- SendCData(" ", 4);
- SendCData(ctl.src_name, strlen(ctl.src_name));
- SendCData(&lf, 1);
- }
- if (!count)
- SendData("\pno entries\012");
- }
-